package com.ld.collegetask.beans;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.databinding.BindingAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ld.collegetask.DynamicMemberInfoActivity;
import com.ld.collegetask.R;
import com.ld.collegetask.login.LoginActivity;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;
import com.mrmo.mimageviewerlib.MImageViewerActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by 樊海波 on 2017/10/18.
 */

public class DynamicBean {

    /**
     * dcomments : 0
     * dphotos :
     * schoolName : 武汉大学
     * dcomplaints : 0
     * dynamicId : 77
     * dcreatedate : 2018-03-02 08:52:01.0
     * dcontent : 432424
     * realname : lx123
     * dfocus : 0
     * dstatus : 1
     * dlikes : 0
     * dynamicUserId:0
     */

    private int dcomments;
    private String dphotos;
    private String schoolName;
    private int dcomplaints;
    private int dynamicId;
    private String dcreatedate;
    private String dcontent;
    private String realname;
    private int dfocus;
    private int dstatus;
    private int dlikes;
    private int dynamicUserId;
    private String userImg;

    public String getUserImg() {
        return userImg;
    }

    public void setUserImg(String userImg) {
        this.userImg = userImg;
    }

    public int getDynamicUserId() {
        return dynamicUserId;
    }

    public void setDynamicUserId(int dynamicUserId) {
        this.dynamicUserId = dynamicUserId;
    }

    @BindingAdapter({"bind:OrderUserUserImg"})
    public static void OrderUserUserImg(ImageView view, String imageUrl) {
        Picasso.with(view.getContext())
                .load(AppApplication.server+imageUrl)
                .placeholder(R.drawable.user_img)
                .fit()
                .into(view);
    }

    public void click(View view) {
        Activity mActivity = (Activity) view.getContext();
        SharePreference sp = new SharePreference(mActivity);
        if (sp.getLoginState()) {
            Intent it = new Intent(mActivity, DynamicMemberInfoActivity.class);
            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            it.putExtra("dynamicId", dynamicId);
            mActivity.startActivityForResult(it, 1);
        }else {
            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
        }
    }

    public void click1(View view) {
        if (dphotos != null) {
            Activity mActivity = (Activity) view.getContext();
            ArrayList<String> imageList = new ArrayList<>();
            String[] str = dphotos.split(",");
            for (int i=0; i < str.length; i++)
                imageList.add(AppApplication.server + str[i]);
            Intent intent = new Intent(mActivity, MImageViewerActivity.class);
            intent.putStringArrayListExtra(MImageViewerActivity.PARAM_LIST_IMAGE_DATA, imageList);
            intent.putExtra(MImageViewerActivity.PARAM_INT_IMAGE_POSITION, 0);
            mActivity.startActivity(intent);
        }
    }

    public void click2(View view) {
        if (dphotos != null) {
            Activity mActivity = (Activity) view.getContext();
            ArrayList<String> imageList = new ArrayList<>();
            String[] str = dphotos.split(",");
            for (int i=0; i < str.length; i++)
                imageList.add(AppApplication.server + str[i]);
            Intent intent = new Intent(mActivity, MImageViewerActivity.class);
            intent.putStringArrayListExtra(MImageViewerActivity.PARAM_LIST_IMAGE_DATA, imageList);
            intent.putExtra(MImageViewerActivity.PARAM_INT_IMAGE_POSITION, 1);
            mActivity.startActivity(intent);
        }
    }

    public void click3(View view) {
        if (dphotos != null) {
            Activity mActivity = (Activity) view.getContext();
            ArrayList<String> imageList = new ArrayList<>();
            String[] str = dphotos.split(",");
            for (int i=0; i < str.length; i++)
                imageList.add(AppApplication.server + str[i]);
            Intent intent = new Intent(mActivity, MImageViewerActivity.class);
            intent.putStringArrayListExtra(MImageViewerActivity.PARAM_LIST_IMAGE_DATA, imageList);
            intent.putExtra(MImageViewerActivity.PARAM_INT_IMAGE_POSITION, 2);
            mActivity.startActivity(intent);
        }
    }

    public int getDcomments() {
        return dcomments;
    }

    public void setDcomments(int dcomments) {
        this.dcomments = dcomments;
    }

    public String getDphotos() {
        return dphotos;
    }

    public void setDphotos(String dphotos) {
        this.dphotos = dphotos;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public int getDcomplaints() {
        return dcomplaints;
    }

    public void setDcomplaints(int dcomplaints) {
        this.dcomplaints = dcomplaints;
    }

    public int getDynamicId() {
        return dynamicId;
    }

    public void setDynamicId(int dynamicId) {
        this.dynamicId = dynamicId;
    }

    public String getDcreatedate() {
        return dcreatedate.split("\\.")[0];
    }

    public void setDcreatedate(String dcreatedate) {
        this.dcreatedate = dcreatedate;
    }

    public String getDcontent() {
        return dcontent;
    }

    public void setDcontent(String dcontent) {
        this.dcontent = dcontent;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public int getDfocus() {
        return dfocus;
    }

    public void setDfocus(int dfocus) {
        this.dfocus = dfocus;
    }

    public int getDstatus() {
        return dstatus;
    }

    public void setDstatus(int dstatus) {
        this.dstatus = dstatus;
    }

    public int getDlikes() {
        return dlikes;
    }

    public void setDlikes(int dlikes) {
        this.dlikes = dlikes;
    }

    public int getVisibility_1() {
        if (dphotos != null) {
            String[] str = dphotos.split(",");
            if (!dphotos.equals("") && str.length >= 1) {
                return View.VISIBLE;
            }
        }
        return View.INVISIBLE;
    }

    public int getVisibility_2() {
        if (dphotos != null) {
            String[] str = dphotos.split(",");
            if (str.length >= 2) {
                return View.VISIBLE;
            }
        }
        return View.INVISIBLE;
    }

    public int getVisibility_3() {
        if (dphotos != null) {
            String[] str = dphotos.split(",");
            if (str.length >= 3) {
                return View.VISIBLE;
            }
        }
        return View.INVISIBLE;
    }

    @BindingAdapter({"bind:titleImgOne"})
    public static void titleImgOne(ImageView view, String imageUrl) {
        if (imageUrl != null) {
            if (!imageUrl.equals("") && imageUrl.split(",").length >= 1) {
                setViewWH(view, 1);
                imageUrl = imageUrl.split(",")[0];
                Picasso.with(view.getContext())
                        .load(AppApplication.server + imageUrl)
                        .placeholder(R.mipmap.gallery_pick_photo)
                        .error(R.mipmap.gallery_pick_photo)
                        .into(view);
            } else {
                setViewWH(view, 0);
            }
        } else {
            setViewWH(view, 0);
        }
    }

    @BindingAdapter({"bind:titleImgTwo"})
    public static void titleImgTwo(ImageView view, String imageUrl) {
        if (imageUrl != null) {
            if (imageUrl.split(",").length >= 2) {
                setViewWH(view, 1);
                imageUrl = imageUrl.split(",")[1];
                Picasso.with(view.getContext())
                        .load(AppApplication.server + imageUrl)
                        .placeholder(R.mipmap.gallery_pick_photo)
                        .error(R.mipmap.gallery_pick_photo)
                        .into(view);
            } else {
                setViewWH(view, 0);
            }
        } else {
            setViewWH(view, 0);
        }
    }

    @BindingAdapter({"bind:titleImgThree"})
    public static void titleImgThree(ImageView view, String imageUrl) {
        if (imageUrl != null) {
            if (imageUrl.split(",").length >= 3) {
                setViewWH(view, 1);
                imageUrl = imageUrl.split(",")[2];
                Picasso.with(view.getContext())
                        .load(AppApplication.server + imageUrl)
                        .placeholder(R.mipmap.gallery_pick_photo)
                        .error(R.mipmap.gallery_pick_photo)
                        .into(view);
            } else {
                setViewWH(view, 0);
            }
        } else {
            setViewWH(view, 0);
        }
    }

    private static void setViewWH(ImageView view, int hei) {
        if (hei != 0) {
            hei = getActivityFromView(view).getResources().getDisplayMetrics().widthPixels / 3 / 4 * 3;
        }
        LinearLayout.LayoutParams para1 = (LinearLayout.LayoutParams) view.getLayoutParams();
        para1.height = hei;
        view.setLayoutParams(para1);
    }

    public static Activity getActivityFromView(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

}
